package com.xianyu.us.controller;

import java.util.Random;

import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.xianyu.us.common.annotatian.ExtCacheable;
import com.xianyu.us.common.utils.RedisUtils;
import com.xianyu.us.common.utils.SendMsgUtils;
import com.xianyu.us.pojo.entity.UsUser;
import com.xianyu.us.pojo.vo.JsonResult;
import com.xianyu.us.service.UsUserService;

/**
 * 注册相关入口
 * @author wangshili
 *
 */
@RestController
@RequestMapping("/user/")
public class UsUserController {
	
	@Autowired
	private UsUserService usUserService;
	
	/**
	 * 处理客户端的登陆请求
	 * "记住我"功能
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping("doLogin")
	public JsonResult doLogin(String username,String password,boolean isRememberMe) {
		UsernamePasswordToken token = new UsernamePasswordToken(username,password);
		//设置记住我功能
		token.setRememberMe(isRememberMe);
		
		Subject currentUser = SecurityUtils.getSubject();
		currentUser.login(token);//将用户信息提交给securitymanager进行认证
		return new JsonResult("登陆成功");
	}
	

	/**
	 * 创建用户
	 * @param user  填写用户信息
	 * @param phoneCode 验证码
	 * @return
	 */
	@RequestMapping("addUser")
	public JsonResult doAddUser(UsUser user,String code) {
		usUserService.insertUser(user, code);
		return new JsonResult("创建成功"); 
	}
	
	/**
	 * 获取手机验证码，把验证码发给前端
	 * @return
	 */
	@RequestMapping("getPhoneCode")
	public JsonResult getPhoneCode(String phone) {
		System.out.println(phone);
		System.out.println(usUserService.getPhoneCode(phone));
		return new JsonResult("已发送短信");
	}
	

	/**
	 * 获取邮件
	 * @param mail 邮件号
	 * @return
	 */
	@RequestMapping("getMailCode")
	public JsonResult sendMail(String mail) {//toSend需要发送的邮箱
		usUserService.getMailCode(mail);
		System.out.println(usUserService.getMailCode(mail));
		return new JsonResult("已发送邮件");
	}
}
